/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.recharge.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.recharge.dao.RechargeCacheDao;
import com.boxin.ims.modules.recharge.entity.RechargeCache;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 推荐奖励储值Service
 * @author XiaoQian
 * @version 2014-04-09
 */
@Component
@Transactional(readOnly = true)
public class RechargeCacheService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(RechargeCacheService.class);
	
	@Autowired
	private RechargeCacheDao rechargeCacheDao;
	
	public RechargeCache get(Long id) {
		return rechargeCacheDao.findOne(id);
	}
	
	public Page<RechargeCache> find(Page<RechargeCache> page, RechargeCache rechargeCache) {
		DetachedCriteria dc = rechargeCacheDao.createDetachedCriteria();
		dc.createAlias("parentMember", "member");
		if (rechargeCache.getUser()!=null && rechargeCache.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", rechargeCache.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(rechargeCache.getName())){
			dc.add(Restrictions.like("name", "%"+rechargeCache.getName()+"%"));
		}
		if (rechargeCache.getParentMember()!=null &&StringUtils.isNotEmpty(rechargeCache.getParentMember().getMobile())){
			dc.add(Restrictions.eq("member.mobile", rechargeCache.getParentMember().getMobile()));
		}
		dc.addOrder(Order.desc("id"));
		return rechargeCacheDao.find(page, dc);
	}
	
	
	public List<RechargeCache> find(RechargeCache rechargeCache) {
		DetachedCriteria dc = rechargeCacheDao.createDetachedCriteria();
		dc.createAlias("parentMember", "member");
		dc.createAlias("childMember", "childMember");
		if (rechargeCache.getUser()!=null && rechargeCache.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", rechargeCache.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(rechargeCache.getName())){
			dc.add(Restrictions.like("name", "%"+rechargeCache.getName()+"%"));
		}
		if (rechargeCache.getParentMember()!=null &&StringUtils.isNotEmpty(rechargeCache.getParentMember().getMobile())){
			dc.add(Restrictions.eq("member.mobile", rechargeCache.getParentMember().getMobile()));
		}
		if(rechargeCache.getChildMember() != null && rechargeCache.getChildMember().getId()>0){
			dc.add(Restrictions.eq("childMember.id", rechargeCache.getChildMember().getId()));
		}
		
		if(com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(rechargeCache.getStatus())){
			dc.add(Restrictions.eq("status",rechargeCache.getStatus()));
		}
		
		dc.addOrder(Order.desc("id"));
		return rechargeCacheDao.find(dc);
	}
	
	
	
	@Transactional(readOnly = false)
	public void save(RechargeCache rechargeCache) {
		rechargeCacheDao.save(rechargeCache);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		rechargeCacheDao.deleteById(id);
	}
	
	@Transactional(readOnly = false)
	public void updateStatusById(Long id) {
		rechargeCacheDao.updateStatusById(id);
	}
	
	/**
	 * name  为父类名字
	 *@author xiaoqian
	 * @param name
	 * 2014-4-9上午11:01:58
	 */
	public void saveRechargeCache(String name,Long user_id,Integer money,Integer score,Long parentMemberId,Long childMemberId){
		RechargeCache rechargeCache = new RechargeCache();
		rechargeCache.setName(name);
		User user =new User();
		user.setId(user_id);
		rechargeCache.setUser(user);
		WechatMember parentMember=new WechatMember();
		WechatMember childMember=new WechatMember();
		parentMember.setId(parentMemberId);
		childMember.setId(childMemberId);
		
		rechargeCache.setParentMember(parentMember);
		rechargeCache.setChildMember(childMember);
		
		rechargeCache.setMoney(money);
		rechargeCache.setCreateTime(new Date());
		rechargeCache.setType("0");
		rechargeCache.setStatus(rechargeCache.NOT_ACTIVATE);
		
		rechargeCache.setScore(score);
		
		rechargeCacheDao.save(rechargeCache);
	}
	
}
